0
תגובות
טפסי כניסה ויציאה וחנות
פתח
phpon
,
<?php
session_start();
define( "USERNAME", "john" );
define( "PASSWORD", "secret" );
if ( isset( $_POST["login"] ) ) {
login();
} elseif ( isset( $_GET["action"] ) and $_GET["action"] == "logout" ) {
logout();
} elseif ( isset( $_SESSION["username"] ) ) {
displayPage();
} else {
displayLoginForm();
}
function login() {
if ( isset( $_POST["username"] ) and isset( $_POST["password"] ) ) {
if ( $_POST["username"] == USERNAME and $_POST["password"] == PASSWORD ) {
$_SESSION["username"] == USERNAME;
session_write_close();
header( "Location: login.php" );
} else {
displayLoginForm( "Sorry, that username/password could not be found. Please
try again." );
}
}
}
function logout() {
unset( $_SESSION["username"] );
session_write_close();
header( "Location: login.php" );
}
function displayPage() {
displayPageHeader();
?>
<p> Welcome,<strong><?php echo $_SESSION["username"] ?></strong> ! You are
currently logged in. </p>
<p><a href="login.php?action=logout">Logout</a></p>
</body>
</html>
<?php
}
function displayLoginForm( $message="" ) {
displayPageHeader();
?>
<?php if ( $message ) echo '<p class="error">'.$message . '</p>' ?>
<form action="login.php" method="post">
<div style="width: 30em;">
<label for="username">UserName</label>
<input type="text"name="username"id="username"value=""/>
<label for="password">Password</label>
<input type="password"name="password"id="password"value=""/>
<div style="clear:both;">
<input type="submit" name="login" value="Login" />
</div>
</div>
</form>
</body>
</html>
<?php
}
function displayPageHeader() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Membership Form</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
.error {background: #d33; color: white; padding: 0.2em;}
</style>
</head>
<body>
<h1>A login/logout system</h1>
<?php
}
?>
session_start();
define( "USERNAME", "john" );
define( "PASSWORD", "secret" );
if ( isset( $_POST["login"] ) ) {
login();
} elseif ( isset( $_GET["action"] ) and $_GET["action"] == "logout" ) {
logout();
} elseif ( isset( $_SESSION["username"] ) ) {
displayPage();
} else {
displayLoginForm();
}
function login() {
if ( isset( $_POST["username"] ) and isset( $_POST["password"] ) ) {
if ( $_POST["username"] == USERNAME and $_POST["password"] == PASSWORD ) {
$_SESSION["username"] == USERNAME;
session_write_close();
header( "Location: login.php" );
} else {
displayLoginForm( "Sorry, that username/password could not be found. Please
try again." );
}
}
}
function logout() {
unset( $_SESSION["username"] );
session_write_close();
header( "Location: login.php" );
}
function displayPage() {
displayPageHeader();
?>
<p> Welcome,<strong><?php echo $_SESSION["username"] ?></strong> ! You are
currently logged in. </p>
<p><a href="login.php?action=logout">Logout</a></p>
</body>
</html>
<?php
}
function displayLoginForm( $message="" ) {
displayPageHeader();
?>
<?php if ( $message ) echo '<p class="error">'.$message . '</p>' ?>
<form action="login.php" method="post">
<div style="width: 30em;">
<label for="username">UserName</label>
<input type="text"name="username"id="username"value=""/>
<label for="password">Password</label>
<input type="password"name="password"id="password"value=""/>
<div style="clear:both;">
<input type="submit" name="login" value="Login" />
</div>
</div>
</form>
</body>
</html>
<?php
}
function displayPageHeader() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Membership Form</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
.error {background: #d33; color: white; padding: 0.2em;}
</style>
</head>
<body>
<h1>A login/logout system</h1>
<?php
}
?>
לא נותן לי תאפשרות לצאת.
<?php
session_start();
class Product {
private $productId;
private $productName;
private $price;
public function __construct( $productId, $productName, $price ) {
$this->productId = $productId;
$this->productName = $productName;
$this->price = $price;
}
public function getId() {
return $this->productId;
}
public function getName() {
return $this->productName;
}
public function getPrice() {
return $this->price;
}
}
$products = array(
1 => new Product( 1, "superWidget", 19.99 ),
2 => new Product( 2, "megaWidget", 29.99 ),
3 => new Product( 3, "wonderWidget", 39.99 )
);
if ( !isset( $_SEESION["cart"] ) ) $_SESSION["cart"] = array();
if ( isset( $_GET["action"] ) and $_GET["action"] == "addItem" ) {
addItem();
} elseif ( isset( $_GET["action"] ) and $_GET["action"] == "removeItem" ) {
removeItem();
} else {
displayCart();
}
function addItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( !isset( $_SESSION["cart"][$productId] ) ) {
$_SESSION["cart"][$productId] = $products[$productId];
}
}
session_write_close();
header( "Location: shopping_cart.php" );
}
function removeItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and
$_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( isset( $_SESSION["cart"][$productId] ) ) {
unset( $_SESSION["cart"][$productId] );
}
}
session_write_close();
header( "Loctaion: shopping_cart.php" );
}
function displayCart() {
global $products;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Membership Form</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1>Your Shopping cart</h1>
<dl>
<?php
$totalPrice = 0;
foreach ( $_SESSION["cart"] as $product ) {
$totalPrice += $product->getPrice();
?>
<dt><?php echo $product->getName() ?></dt>
<dd>$<?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=removeItem&productId=<?php echo
$product->getId() ?>">Remove</a></dd>
<?php } ?>
<dt>Cart Total:</dt>
<dd><strong>$<?php echo number_format( $totalPrice, 2 ) ?></strong>
</dd>
</dl>
<h1>Product list</h1>
<dl>
<?php foreach ( $products as $product ) { ?>
<dt><?php echo $product->getName() ?></dt>
<dd>$<?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=addItem&productId=<?php echo
$product->getId() ?>"> Add Item</a></dd>
<?php } ?>
</dl>
<?php } ?>
</body>
</html>
session_start();
class Product {
private $productId;
private $productName;
private $price;
public function __construct( $productId, $productName, $price ) {
$this->productId = $productId;
$this->productName = $productName;
$this->price = $price;
}
public function getId() {
return $this->productId;
}
public function getName() {
return $this->productName;
}
public function getPrice() {
return $this->price;
}
}
$products = array(
1 => new Product( 1, "superWidget", 19.99 ),
2 => new Product( 2, "megaWidget", 29.99 ),
3 => new Product( 3, "wonderWidget", 39.99 )
);
if ( !isset( $_SEESION["cart"] ) ) $_SESSION["cart"] = array();
if ( isset( $_GET["action"] ) and $_GET["action"] == "addItem" ) {
addItem();
} elseif ( isset( $_GET["action"] ) and $_GET["action"] == "removeItem" ) {
removeItem();
} else {
displayCart();
}
function addItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( !isset( $_SESSION["cart"][$productId] ) ) {
$_SESSION["cart"][$productId] = $products[$productId];
}
}
session_write_close();
header( "Location: shopping_cart.php" );
}
function removeItem() {
global $products;
if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and
$_GET["productId"] <= 3 ) {
$productId = (int) $_GET["productId"];
if ( isset( $_SESSION["cart"][$productId] ) ) {
unset( $_SESSION["cart"][$productId] );
}
}
session_write_close();
header( "Loctaion: shopping_cart.php" );
}
function displayCart() {
global $products;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Membership Form</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1>Your Shopping cart</h1>
<dl>
<?php
$totalPrice = 0;
foreach ( $_SESSION["cart"] as $product ) {
$totalPrice += $product->getPrice();
?>
<dt><?php echo $product->getName() ?></dt>
<dd>$<?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=removeItem&productId=<?php echo
$product->getId() ?>">Remove</a></dd>
<?php } ?>
<dt>Cart Total:</dt>
<dd><strong>$<?php echo number_format( $totalPrice, 2 ) ?></strong>
</dd>
</dl>
<h1>Product list</h1>
<dl>
<?php foreach ( $products as $product ) { ?>
<dt><?php echo $product->getName() ?></dt>
<dd>$<?php echo number_format( $product->getPrice(), 2 ) ?>
<a href="shopping_cart.php?action=addItem&productId=<?php echo
$product->getId() ?>"> Add Item</a></dd>
<?php } ?>
</dl>
<?php } ?>
</body>
</html>
לא מציג לי את האפשרויות שאפשר לקנות..ולא מעלה את הסל שקנית ואת מה שקניתי.
אין פה שימוש בקבצים/מסד.
בקרוב אני אתחיל לעבוד איתם אבל ביינתים זה מה שיש.. (:
אם יש למישהו יש הצעות איך לתרגל סש/עוגיות עם פוסט אניאשמח להצעות.